using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SkinItem : MonoBehaviour
{
	public delegate void SkinItemAction(int itemIndex, int skinIndex);

	public SkinType skinType;

	public Text itemName;

	public Image itemBackImage;

	public Sprite itemBackSprite;

	public Sprite itemBackHoverSprite;

	public Button selectButton;

	public Image itemImage;

	public GameObject selectedPanel;

	public int itemIndex;

	public int skinIndex;

	protected string openedTag
	{
		get
		{
			return string.Format("Skin_{0}", GameManager.Instance.FidgetStore.fidgetTypes[skinIndex].name);
		}
	}

	public bool Opened
	{
		get
		{
			return PlayerPrefs.GetInt(openedTag, 0) == 1;
		}
		set
		{
			PlayerPrefs.SetInt(openedTag, value ? 1 : 0);
		}
	}

	public bool selected
	{
		get
		{
			return GameManager.Instance.SelectedItemIndex == itemIndex;
		}
	}

	public static event SkinItemAction OnSkinItemSelected;

	public void FireItemSelectEvent()
	{
		if (SkinItem.OnSkinItemSelected != null)
		{
			SkinItem.OnSkinItemSelected(itemIndex, skinIndex);
		}
	}

	protected virtual void Awake()
	{
		itemImage.sprite = GameManager.Instance.FidgetStore.fidgetTypes[skinIndex].fidgetLevels[0];
		itemName.text = GameManager.Instance.FidgetStore.fidgetTypes[skinIndex].name;
		selectButton.onClick.AddListener(FireItemSelectEvent);
		itemImage.GetComponent<Animator>().Play("rotate", 0, Random.value);
		itemImage.GetComponent<Animator>().SetFloat("speed", Random.Range(0.5f, 1.2f));
	}

	public virtual void OpenItem()
	{
		Opened = true;
		FireItemSelectEvent();
		SkinCollection.Instance.IndexList.Add(skinIndex);
		AnalyticManager.SendEvent(string.Format("Opened_Skin_{0}", GameManager.Instance.FidgetStore.fidgetTypes[skinIndex].name));
	}

	public virtual void SelectItem()
	{
		itemBackImage.sprite = itemBackHoverSprite;
		selectedPanel.SetActive(true);
		selectButton.gameObject.SetActive(false);
	}

	public virtual void DeselectItem()
	{
		itemBackImage.sprite = itemBackSprite;
		selectedPanel.SetActive(false);
		selectButton.gameObject.SetActive(true);
	}
}
